package com.example.combwyaq.mvp.presenter;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.combwyaq.App;
import com.example.combwyaq.entity.TokenEntity;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.contract.SplashContract;
import com.example.combwyaq.utils.LogUtils;
import com.example.combwyaq.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashPresenter extends BasePresenter<SplashContract.ISplashModel, SplashContract.ISplashView> {
    @Inject
    public SplashPresenter(SplashContract.ISplashModel iSplashModel, SplashContract.ISplashView iSplashView) {
        super(iSplashModel, iSplashView);
    }
    public void requestToken(){
        Map<String, Object> map = new HashMap<>();
        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(),
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        String imei = PhoneUtils.getIMEI();

        map.put("imie",imei);
        m.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onNext);


    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        TokenEntity entity = (TokenEntity) baseEntity;
        LogUtils.e("SplashPresenter->onNext");
        if (entity.status.equals("200")){
            //保存token到本地SP存储中
            SpUtils.saveData("token",entity.getValues().token);
            LogUtils.e("SplashPresenter->onNext1");
            //通知activity界面跳转
            v.refresh();
        }
    }
}
